package com.chixing.day15_map_file;

import com.chixing.day14_map.Student;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        //根据key排序
        TreeMap<String,Integer> map = new TreeMap<>();
        map.put("sm",1254);
        map.put("sj",2232);
        map.put("sp",1334);
        map.put("sp",2334);
        map.put("yp",1254);
        System.out.println(map);

        TreeMap<Product,Integer> productMap = new TreeMap<>();
        productMap.put(new Product(101,"h华为p30",4399.9),4585);
        productMap.put(new Product(102,"h华为p30",6399.9),3485);
        productMap.put(new Product(103,"h华为p30",4399.9),3485);
        productMap.put(new Product(102,"h华为p30",2399.9),3485);
        productMap.put(new Product(104,"p苹果13",8399.9),5585);
        System.out.println(productMap);

        TreeMap<Product,Integer> treeMap =  new TreeMap<>(new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                if (o1 == o2)
                    return 0;
                if (!o1.getProName().equals(o2.getProName()))
                    return o1.getProName().compareTo(o2.getProName());
                if (!(o1.getProPrice()==o2.getProPrice()))
                    return (Double.valueOf(o1.getProPrice()).compareTo(o2.getProPrice()));
                return o1.getProId() - o2.getProId();
            }
        });
        treeMap.put(new Product(101,"h华为p30",4399.9),3485);
        treeMap.put(new Product(102,"r华为p30",4399.9),3485);
        treeMap.put(new Product(103,"h华为p30",3399.9),3485);
        System.out.println(treeMap);

        TreeMap<Student,Integer> stuMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getStuTime().compareTo(o2.getStuTime());
            }
        });


    }
}

//Product 实现了自然顺序
class Product implements Comparable<Product>{

    //定义比较规则，实现先后顺序的比较
    //先比较naem的字母顺序， 若name 相同，比较价格，若价格相同，比较id
    @Override
    public int compareTo(Product o) {
        if (this == o)
            return 0;
        if (!this.proName.equals(o.proName))
            return this.proName.compareTo(o.proName);
        if (!(this.proPrice==o.proPrice))
            return (Double.valueOf(this.proPrice).compareTo(o.proPrice));
        return this.proId - o.proId;
    }
    private int proId;
    private String proName;
    private double proPrice;

    @Override
    public String toString() {
        return "Product{" +
                "proId=" + proId +
                ", proName='" + proName + '\'' +
                ", proPrice=" + proPrice +
                '}';
    }

    public int getProId() {
        return proId;
    }

    public void setProId(int proId) {
        this.proId = proId;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public double getProPrice() {
        return proPrice;
    }

    public void setProPrice(double proPrice) {
        this.proPrice = proPrice;
    }



    public Product(int proId, String proName, double proPrice) {
        this.proId = proId;
        this.proName = proName;
        this.proPrice = proPrice;
    }


}